document.write("<script language=javascript src='js/md5.js'></script>");
var ip_port = 'http://120.25.151.57:8080/';
//ip_port = 'http://192.168.199.243:8080/';
var key_header = '!!PSKEYAPP';
var version = '1.1.9';
var work_status_bollean = false;
//默认请求间隔，防止session过期，10分钟
var request_gap = 5*60*1000;
var sign_tip = '请打上班卡，然后再上报数据';
var error_tip = '提交数据失败，可能由于网络信号不好或服务器正在维修，请到信号好的地方重试或者等待服务器维修完成重试';
var $_GET = (function() {
	var url = window.document.location.href.toString();
	var u = url.split("?");
	if(typeof(u[1]) == "string") {
		u = u[1].split("&");
		var get = {};
		for(var i in u) {
			var j = u[i].split("=");
			get[j[0]] = j[1];
		}
		return get;
	} else {
		return {};
	}
})();
(function(mui, doc) {

	mui.plusReady(function() {
		mui.back = function(event) {
			var btnArray = ['是', '否'];
			mui.confirm("确认退出？", "提示", btnArray, function(e) {
				if(e.index == 0) {
					plus.runtime.quit();
				} else {
					return;
				}
			}, 'div')
		};
		if(window.location.pathname.indexOf('/index.html') < 0){
			return;
		}

		//退出按钮
		doc.getElementById("logout").addEventListener("tap", function() {
			var url = ip_port + '/Manage/Mobile/logout';
			mui.ajax(url, {
				dataType: 'json', //服务器返回json格式数据
				type: 'get', //HTTP请求类型
				success: function(data) {
					plus.storage.removeItem("uid");
					window.location.href = 'login.html';
				},
				error: function(xhr, type, errorThrown) {
					//异常处理；
					mui.alert(error_tip, "提示");
				}
			});
		});

		//添加现场值班照片
		doc.getElementById("btn_uploadimage").addEventListener("click", function() {
			appendByCamera()
		});

		//检查网络连接状态
		//		if(plus.networkinfo.CONNECTION_NONE == 1){
		//			mui.alert('请打开网络连接', '提示');
		//		}
		//上下班状态
		//默认为下班
		set_work_status();
		set_tab_enable();
		//选中tab
		var tabb_id = $_GET['tabb_id']?$_GET['tabb_id']:'sign_tab'
		mui.trigger(document.getElementById(tabb_id), 'touchstart');
		mui.trigger(document.getElementById(tabb_id), 'tap');
		doc.getElementById('user_realname').innerText = '当前用户：'+plus.storage.getItem('realname');
	});
	
	//默认请求，每隔多长时间请求一次，防止seesion过期
//	window.setInterval("ajax_reqest_session()",request_gap);

}(mui, document));

/**
 * 检查客户端版本
 */
function check_version(){
	mui.ajax(ip_port + '/Manage/Mobile/check_mobile_version', {
		async:false,
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		beforeSend: function(xhr) {
	        xhr.setRequestHeader("KEY", key_header);  
	    },
		success: function(data) {
			if(data.version != version) {
//				document.getElementById('login').style.display = 'none';
				var btnArray = ['是','否'];
				mui.confirm('有新版本是否下载？', '新版本提示', btnArray, function(e) {
					if (e.index == 0) {
						download_apk();
					}else if (e.index == 1) {
						
					}
				}, 'div');
			}
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			mui.alert(error_tip, "提示");
		}
	});
}

function deleteFilesByDirectory() {
	var wt = plus.nativeUI.showWaiting("正在清理数据...");
	//删除Download下的文件
	plus.io.requestFileSystem(plus.io.PUBLIC_DOWNLOADS, function(fs) {		
		fs.root.removeRecursively(function(entry) {
			plus.nativeUI.closeWaiting();
		}, function(e) {
			plus.nativeUI.closeWaiting();
		});
	}, function(e) {
		plus.nativeUI.closeWaiting();
	});
	//删除DOC下的文件
	wt = plus.nativeUI.showWaiting("正在清理图像...");
	plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function(fs) {
		fs.root.removeRecursively(function(entry) {
			plus.nativeUI.closeWaiting();
		}, function(e) {
			plus.nativeUI.closeWaiting();
		});
	}, function(e) {
		plus.nativeUI.closeWaiting();
	});	
}

function set_tab_enable() {
	var wt = plus.nativeUI.showWaiting("正在加载...");
	var uid = plus.storage.getItem('uid');
	var stations = plus.storage.getItem('stations');
	var csdts = plus.storage.getItem('csdts');
	var gates = plus.storage.getItem('gates');
	var url = ip_port + '/Manage/Mobile/set_tab_enable?uid='+uid+'&stations='+stations+'&csdts='+csdts+'&gates='+gates;
	mui.ajax(url, {
		async:false,
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		beforeSend: function(xhr) {
	        xhr.setRequestHeader("KEY", key_header);  
	    },
		success: function(data) {
			if(data.station != 1) {
				document.getElementById('pumpstation_tab').href = '';
			}
			if(data.csdt != 1) {
				document.getElementById('csdt_tab').href = '';
			}
			if(data.gate != 1) {
				//打开河道闸门填报记录
				document.getElementById('gate_tab').href = '';
			}
			wt.close();
		},
		error: function(xhr, type, errorThrown) {
			wt.close();
			//异常处理；
			mui.alert(error_tip, "提示");
		}
	});
}

//获取上下班状态
function set_work_status() {
//	var wt = plus.nativeUI.showWaiting("正在设置...");
	var uid = plus.storage.getItem('uid');
	var status = plus.storage.getItem('work_status');
	console.log(status);
	if(status == 1) {
		document.getElementById('btn_uploadimage').src = "image/sbqd.png";
	} else {
		document.getElementById('btn_uploadimage').src = "image/xbqd.png";
	}
}

function get_work_status(){
	var wt = plus.nativeUI.showWaiting("正在验证打卡状态...");
	var status = plus.storage.getItem('work_status');
	wt.close();
	if(status == 1){
		//处于下班状态
		return false;
	}else {
		return true;
	}
}

function ajax_reqest_session(){
	var radom = Math.random();
	var uid = plus.storage.getItem('uid');
	mui.ajax(ip_port + '/Manage/Mobile/get_sign_status?radom='+radom+'&uid='+uid, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型on(data) 
		beforeSend: function(xhr) {
	        xhr.setRequestHeader("KEY", key_header);  
	    },
		success: function(data) {
			console.log(data);
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			mui.alert(error_tip, "提示");
			wt.close();
		}
	});
}

//登录
function login() {
	check_version();
	logout_clean_cache();
	document.getElementById("login").addEventListener("click", function() {
		var b = new Base64();
		var user = document.getElementById("account").value;
//		user = b.encode(user);
		var pwd = document.getElementById("password").value;
//		pwd = b.encode(pwd);
		var scrit = get_scrit();
		mui.ajax(ip_port + '/manage/mobile/login_action', {
			data: {
				'username': user,
				'password': pwd,
				'pinxh': scrit.scrit,
				'jinxh': scrit.time,
			},
			dataType: 'json',
			type: 'get',
			success: function(data) {
				if(data.status == 1) {
					plus.storage.setItem('work_status', data.work_status.toString());
					plus.storage.setItem('uid', data.uid);
					plus.storage.setItem('realname', data.realname);
					plus.storage.setItem('stations', data.stations);
					plus.storage.setItem('csdts', data.csdts);
					plus.storage.setItem('gates', data.gates);
					window.location.href = 'index.html';
				} else {
					mui.alert(data.msg, '错误提示', function() {}, 'div');
				}
			},
			error: function(data) {
//				console.log(data.responseText);
				mui.alert(error_tip, "提示");
			}
		});

	});
}

/**
 * 获取泵站泵机列表
 */
function get_pump_pump_list() {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var uid = plus.storage.getItem('uid');
	var stations = plus.storage.getItem('stations');
	var xxx_url = ip_port + '/Manage/Mobile/get_pump?model=station&uid='+uid+'&stations='+stations;
	var wt = plus.nativeUI.showWaiting("正在加载...");
	mui.ajax(xxx_url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			add_pump_pump_item_from_list(data);
			wt.close();
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

/**
 * 添加泵站泵机
 * @param {Object} list
 */
function add_pump_pump_item_from_list(list) {
	var str = '';
	for(var i = 0; i < list.length; i++) {
		var obj = list[i];
		var color = '';
		if(obj.status == 1){
			color = ' style="background:#ADFF2F;"';
		}else if(obj.status == -2){
			color = ' style="background:#FFFF00;"';
		}
		str += '<li class="mui-table-view-cell mui-media mui-col-xs-6 mui-col-sm-6 cell-height"'+color+'>';
		str += '<a href="bz_bj.html?model=' + obj.model + '&id=' + obj.id + '">';
		str += '<span class="mui-icon">';
		if(obj.type == 1) {
			//雨水泵
			str += '<img src="image/ysb.png" />';
		} else if(obj.type == 2) {
			//污水泵
			str += '<img src="image/wsb.png" />';
		} else {
			//污水泵
			str += '<img src="image/wsb.png" />';
		}
		str += '</span>';
		str += '<div class="mui-media-body cell-font" style="font-size:20px;height:60px;line-height:30px;"><span>' + obj.station + '</span><br><span>' + obj.p_number + '</span></div>';
		str += '</a>';
		str += '</li>';
	}
	document.getElementById('ul_li').innerHTML += str;
}

/**
 * 获取泵站泵机对象
 */
function get_pump_pump_json_obj(model, id) {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var wt = plus.nativeUI.showWaiting("正在加载...");
	var uid = plus.storage.getItem('uid');
	var temp_url = ip_port + '/Manage/Mobile/get_json_obj?model='+model+'&id='+id+'&uid='+uid;
	mui.ajax(temp_url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			document.getElementById('station').innerText += data.station;
			document.getElementById('number').innerText += data.p_number;
			document.getElementById('pid').value += data.id;
			document.getElementById('model').value += $_GET['model'];
			//			set_statuss(data.statuss);
			set_dvice_status_switch(data.status);
			//设定时间
			set_datetime_picker(data.p_auto);
			wt.close();
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

/**
 * 设定设备状态
 */
function set_statuss(statuss) {
	var str = '';
	for(var i = 0; i < statuss.length; i++) {
		var obj = statuss[i];
		//		str += '<label class="con-label" for="status_'+obj.status+'">'+obj.comment+'</label>';
		str += obj.comment;
		if(i == 0) {
			str += '<input class="status-input" name="status" type="radio" id="status_' + obj.status + '" value="' + obj.status + '" checked="checked">';
		} else {
			str += '<input class="status-input" name="status" type="radio" id="status_' + obj.status + '" value="' + obj.status + '">';
		}
	}
	document.getElementById('statuss').innerHTML += str;
}

/**
 * 提交泵站泵机数据
 */
function save_pump_pump() {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var data = $('#save_form').serializeJson();
	data['c_k'] = '!!PSapp';
	var uid = plus.storage.getItem('uid');
	var temp_url = ip_port + '/Manage/mobile/submit_action?aa=11&uid='+uid;
	mui.ajax(temp_url, {
		'data': data,
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		beforeSend: function(xhr) {
	        xhr.setRequestHeader("KEY", key_header);  
	    },
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			if(data.status <= 0) {
				mui.alert(data.msg, "错误提示");
			}
			history.go(-1);
		},
		error: function(data) {
//			console.log(data.responseText);
			mui.alert(error_tip, "提示");
		}
	});
}

/**
 * 获取泵站格栅机列表
 */
function get_pump_machine_list() {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var wt = plus.nativeUI.showWaiting("正在加载...");
	var uid = plus.storage.getItem('uid');
	var stations = plus.storage.getItem('stations');
	var xxx_url = ip_port + '/Manage/Mobile/get_machine?model=station&uid='+uid+'&stations='+stations;
	mui.ajax(xxx_url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		beforeSend: function(xhr) {
	        xhr.setRequestHeader("KEY", key_header);  
	    },
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			add_pump_machine_item_from_list(data);
			wt.close();
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

/**
 * 添加泵站格栅机
 * @param {Object} list
 */
function add_pump_machine_item_from_list(list) {
	var str = '';
	for(var i = 0; i < list.length; i++) {
		var obj = list[i];
		var color = '';
		if(obj.status == 1){
			color = ' style="background:#ADFF2F;"';
		}else if(obj.status == -2){
			color = ' style="background:#FFFF00;"';
		}
		str += '<li class="mui-table-view-cell mui-media mui-col-xs-6 mui-col-sm-6" '+color+'>';
		str += '<a href="bz_gsj.html?model=' + obj.model + '&id=' + obj.id + '">';
		str += '<span class="mui-icon">';
		str += '<img src="image/gsj-l.png" />';
		str += '</span>';
		str += '<div class="mui-media-body" style="font-size:20px;height:60px;line-height:30px;">' + obj.station + '</span><br><span>' + obj.scrmach_number + '</div>';
		str += '</a>';
		str += '</li>';
	}
	document.getElementById('ul_li').innerHTML += str;
}

/**
 * 获取泵站格栅机对象
 */
function get_pump_machine_json_obj(model, id) {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var wt = plus.nativeUI.showWaiting("正在加载...");
	var uid = plus.storage.getItem('uid');
	var temp_url = ip_port + '/Manage/Mobile/get_json_obj?model=' + model + '&id=' + id+'&uid='+uid;
	console.log(temp_url);
	mui.ajax(temp_url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			document.getElementById('station').innerText += data.station;
			document.getElementById('number').innerText += data.scrmach_number;
			document.getElementById('pid').value += data.id;
			document.getElementById('model').value += $_GET['model'];
			//			set_statuss(data.statuss);
			set_dvice_status_switch(data.status);
			wt.close();
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

/**
 * 提交泵站个杀鸡数据
 */
function save_pump_machine() {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var data = $('#save_form').serializeJson();
	data['c_k'] = '!!PSapp';
	var uid = plus.storage.getItem('uid');
	var temp_url = ip_port + '/Manage/mobile/submit_action?aa=11&uid='+uid;
	mui.ajax(temp_url, {
		'data': data,
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		beforeSend: function(xhr) {
	        xhr.setRequestHeader("KEY", key_header);  
	    },
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			if(data.status <= 0) {
				mui.alert(data.msg, "错误提示");
			}
			history.go(-1);
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

/**
 * 获取泵站格栅机列表
 */
function get_pump_waterele_list() {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var wt = plus.nativeUI.showWaiting("正在加载...");
	var uid = plus.storage.getItem('uid');
	var stations = plus.storage.getItem('stations');
	var xxx_url = ip_port + '/Manage/Mobile/get_waterele?model=station&uid='+uid+'&stations='+stations;
	mui.ajax(xxx_url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		success: function(data) {
//			if(data.status == -999){
//				mui.alert("登录超时请重新登录", "提示");
//				window.location = 'login.html';
//			}
			console.log(data);
			add_pump_waterele_item_from_list(data);
			wt.close();
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

/**
 * 添加泵站格栅机
 * @param {Object} list
 */
function add_pump_waterele_item_from_list(list) {
	var str = '';
	for(var i = 0; i < list.length; i++) {
		var obj = list[i];
		str += '<li class="mui-table-view-cell mui-media mui-col-xs-6 mui-col-sm-6">';
		str += '<a href="bz_sdb.html?model=' + obj.model + '&id=' + obj.id + '">';
		str += '<span class="mui-icon">';
		str += '<img src="image/sdb-q.png" />';
		str += '</span>';
		str += '<div class="mui-media-body" style="font-size:20px;height:60px;line-height:30px;">' + obj.station + '</span><br><span>' + obj.we_number + '</div>';
		str += '</a>';
		str += '</li>';
	}
	document.getElementById('ul_li').innerHTML += str;
}

/**
 * 获取泵站格栅机对象
 */
function get_pump_waterele_json_obj(model, id) {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var wt = plus.nativeUI.showWaiting("正在加载...");
	var uid = plus.storage.getItem('uid');
	var temp_url = ip_port + '/Manage/Mobile/get_json_obj?model=' + model + '&id=' + id+'&uid='+uid;
	mui.ajax(temp_url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			document.getElementById('station').innerText += data.station;
			document.getElementById('number').innerText += data.we_number+'('+data.we_name+')';
			document.getElementById('type').innerText += data.type_text;
			document.getElementById('pid').value += data.id;
			document.getElementById('model').value += $_GET['model'];
			wt.close();
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

function get_pump_water_list() {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var wt = plus.nativeUI.showWaiting("正在加载...");
	var uid = plus.storage.getItem('uid');
	var stations = plus.storage.getItem('stations');
	var xxx_url = ip_port + '/Manage/Mobile/get_water?model=station&uid='+uid+'&stations='+stations;
	mui.ajax(xxx_url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		success: function(data) {
//			if(data.status == -999){
//				mui.alert("登录超时请重新登录", "提示");
//				window.location = 'login.html';
//			}
			add_pump_water_item_from_list(data);
			wt.close();
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

function add_pump_water_item_from_list(list) {
	var str = '';
	for(var i = 0; i < list.length; i++) {
		var obj = list[i];
		str += '<li class="mui-table-view-cell mui-media mui-col-xs-6 mui-col-sm-6">';
		str += '<a href="bz_sb.html?model=' + obj.model + '&id=' + obj.id + '">';
		str += '<span class="mui-icon">';
		str += '<img src="image/sdb-q.png" />';
		str += '</span>';
		str += '<div class="mui-media-body" style="font-size:20px;height:60px;line-height:30px;">' + obj.station + '</span><br><span>' + obj.we_number + '</div>';
		str += '</a>';
		str += '</li>';
	}
	document.getElementById('ul_li').innerHTML += str;
}

function get_pump_water_json_obj(model, id) {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var wt = plus.nativeUI.showWaiting("正在加载...");
	var uid = plus.storage.getItem('uid');
	var temp_url = ip_port + '/Manage/Mobile/get_json_obj?model=' + model + '&id=' + id+'&uid='+uid;
	mui.ajax(temp_url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			document.getElementById('station').innerText += data.station;
			document.getElementById('number').innerText += data.we_number;
			document.getElementById('pid').value += data.id;
			document.getElementById('model').value += $_GET['model'];
//			set_statuss(data.statuss);
			wt.close();
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

/**
 * 提交数据到服务器
 */
function save_form() {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var wt = plus.nativeUI.showWaiting("正在上传数据...");
	var data = $('#save_form').serializeJson();
	console.log(JSON.stringify(data ));
	data['c_k'] = '!!PSapp';
	var uid = plus.storage.getItem('uid');
	var scrit = get_scrit();
	var temp_url = ip_port + '/Manage/mobile/submit_action?uid='+uid+'&pinxh='+scrit.scrit+'&jinxh='+scrit.time;
	mui.ajax(temp_url, {
		timeout:5000,
		'data': data,
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		beforeSend: function(xhr) {
	        xhr.setRequestHeader("KEY", key_header);  
	    },
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			if(data.status <= 0) {
				mui.alert(data.msg, "错误提示");
			}
			wt.close();
			history.go(-1);
		},
		error: function(data) {
			console.log(data.responseText);
			wt.close();
			mui.alert(error_tip, "提示");
			history.go(-1);
		}
	});
}

/**
 * 获取调蓄池泵机列表
 */
function get_csdt_pump_list() {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var wt = plus.nativeUI.showWaiting("正在加载...");
	var uid = plus.storage.getItem('uid');
	var csdts = plus.storage.getItem('csdts');
	var xxx_url = ip_port + '/Manage/Mobile/get_pump?model=csdt&uid='+uid+'&csdts='+csdts;
	mui.ajax(xxx_url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			add_csdt_pump_item_from_list(data);
			wt.close();
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

/**
 * 添加调蓄池泵机
 * @param {Object} list
 */
function add_csdt_pump_item_from_list(list) {
	var str = '';
	for(var i = 0; i < list.length; i++) {
		var obj = list[i];
		var color = '';
		if(obj.status == 1){
			color = ' style="background:#ADFF2F;"';
		}else if(obj.status == -2){
			color = ' style="background:#FFFF00;"';
		}
		str += '<li class="mui-table-view-cell mui-media mui-col-xs-6 mui-col-sm-6" '+color+'>';
		str += '<a href="txc_bj.html?model=' + obj.model + '&id=' + obj.id + '">';
		str += '<span class="mui-icon">';
		str += '<img src="image/bj.png" />';
		str += '</span>';
		str += '<div class="mui-media-body" style="font-size:20px;height:60px;line-height:30px;">' + obj.station + '</span><br><span>' + obj.p_number + '</div>';
		str += '</a>';
		str += '</li>';
	}
	document.getElementById('ul_li').innerHTML += str;
}

/**
 * 获取调蓄池泵机对象
 */
function get_csdt_pump_json_obj(model, id) {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var wt = plus.nativeUI.showWaiting("正在加载...");
	var uid = plus.storage.getItem('uid');
	var temp_url = ip_port + '/Manage/Mobile/get_json_obj?model=' + model + '&id=' + id+'&uid='+uid;
	mui.ajax(temp_url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			document.getElementById('station').innerText += data.station;
			document.getElementById('number').innerText += data.p_number;
			document.getElementById('pid').value += data.id;
			document.getElementById('model').value += $_GET['model'];
			//			set_statuss(data.statuss);
			set_dvice_status_switch(data.status);
			wt.close();
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

/**
 * 获取调蓄池格栅机列表
 */
function get_csdt_machine_list() {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var wt = plus.nativeUI.showWaiting("正在加载...");
	var uid = plus.storage.getItem('uid');
	var csdts = plus.storage.getItem('csdts');
	var xxx_url = ip_port + '/Manage/Mobile/get_machine?model=csdt&uid='+uid+'&csdts='+csdts;
	mui.ajax(xxx_url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			add_csdt_machine_item_from_list(data);
			wt.close();
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

/**
 * 添加调蓄池泵机
 * @param {Object} list
 */
function add_csdt_machine_item_from_list(list) {
	var str = '';
	for(var i = 0; i < list.length; i++) {
		var obj = list[i];
		var color = '';
		if(obj.status == 1){
			color = ' style="background:#ADFF2F;"';
		}else if(obj.status == -2){
			color = ' style="background:#FFFF00;"';
		}
		str += '<li class="mui-table-view-cell mui-media mui-col-xs-6 mui-col-sm-6" '+color+'>';
		str += '<a href="txc_gsh.html?model=' + obj.model + '&id=' + obj.id + '">';
		str += '<span class="mui-icon">';
		str += '<img src="image/gsj-l.png" />';
		str += '</span>';
		str += '<div class="mui-media-body" style="font-size:20px;height:60px;line-height:30px;">' + obj.station + '</span><br><span>' + obj.scrmach_number + '</div>';
		str += '</a>';
		str += '</li>';
	}
	document.getElementById('ul_li').innerHTML += str;
}

/**
 * 获取调蓄池泵机对象
 */
function get_csdt_machine_json_obj(model, id) {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var wt = plus.nativeUI.showWaiting("正在加载...");
	var uid = plus.storage.getItem('uid');
	var temp_url = ip_port + '/Manage/Mobile/get_json_obj?model=' + model + '&id=' + id+'&uid='+uid;
	mui.ajax(temp_url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			document.getElementById('station').innerText += data.station;
			document.getElementById('number').innerText += data.scrmach_number;
			document.getElementById('pid').value += data.id;
			document.getElementById('model').value += $_GET['model'];
			//			set_statuss(data.statuss);
			set_dvice_status_switch(data.status);
			wt.close();
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

/**
 * 获取调蓄池水电表列表
 */
function get_csdt_waterele_list() {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var wt = plus.nativeUI.showWaiting("正在加载...");
	var uid = plus.storage.getItem('uid');
	var csdts = plus.storage.getItem('csdts');
	var xxx_url = ip_port + '/Manage/Mobile/get_waterele?model=csdt&uid='+uid+'&csdts='+csdts;
	mui.ajax(xxx_url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			add_csdt_waterele_item_from_list(data);
			wt.close();
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

/**
 * 添加调蓄池水电表
 * @param {Object} list
 */
function add_csdt_waterele_item_from_list(list) {
	var str = '';
	for(var i = 0; i < list.length; i++) {
		var obj = list[i];
		str += '<li class="mui-table-view-cell mui-media mui-col-xs-6 mui-col-sm-6">';
		str += '<a href="txc_sdb.html?model=' + obj.model + '&id=' + obj.id + '">';
		str += '<span class="mui-icon">';
		str += '<img src="image/sdb-q.png" />';
		str += '</span>';
		str += '<div class="mui-media-body" style="font-size:20px;height:60px;line-height:30px;">' + obj.station + '</span><br><span>' + obj.we_number + '</div>';
		str += '</a>';
		str += '</li>';
	}
	document.getElementById('ul_li').innerHTML += str;
}

/**
 * 获取调蓄池水电表对象
 */
function get_csdt_waterele_json_obj(model, id) {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var wt = plus.nativeUI.showWaiting("正在加载...");
	var uid = plus.storage.getItem('uid');
	var temp_url = ip_port + '/Manage/Mobile/get_json_obj?model=' + model + '&id=' + id+'&uid='+uid;
	mui.ajax(temp_url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			document.getElementById('station').innerText = data.station;
			document.getElementById('number').innerText = data.we_number+'('+data.we_name+')';
			document.getElementById('type').innerText = data.type_text;
			document.getElementById('pid').value = data.id;
			document.getElementById('model').value = $_GET['model'];
			wt.close();
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

function get_csdt_water_list() {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var wt = plus.nativeUI.showWaiting("正在加载...");
	var uid = plus.storage.getItem('uid');
	var csdts = plus.storage.getItem('csdts');
	var xxx_url = ip_port + '/Manage/Mobile/get_water?model=csdt&uid='+uid+'&csdts='+csdts;
	mui.ajax(xxx_url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			add_csdt_water_item_from_list(data);
			wt.close();
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

function add_csdt_water_item_from_list(list) {
	var str = '';
	for(var i = 0; i < list.length; i++) {
		var obj = list[i];
		str += '<li class="mui-table-view-cell mui-media mui-col-xs-6 mui-col-sm-6">';
		str += '<a href="txc_sb.html?model=' + obj.model + '&id=' + obj.id + '">';
		str += '<span class="mui-icon">';
		str += '<img src="image/sdb-q.png" />';
		str += '</span>';
		str += '<div class="mui-media-body" style="font-size:20px;height:60px;line-height:30px;">' + obj.station + '</span><br><span>' + obj.we_number + '</div>';
		str += '</a>';
		str += '</li>';
	}
	document.getElementById('ul_li').innerHTML += str;
}

function get_csdt_water_json_obj(model, id) {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var wt = plus.nativeUI.showWaiting("正在加载...");
	var uid = plus.storage.getItem('uid');
	var temp_url = ip_port + '/Manage/Mobile/get_json_obj?model=' + model + '&id=' + id+'&uid='+uid;
	mui.ajax(temp_url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			document.getElementById('station').innerText += data.station;
			document.getElementById('number').innerText += data.we_number;
			document.getElementById('pid').value += data.id;
			document.getElementById('model').value += $_GET['model'];
//			set_statuss(data.statuss);
			wt.close();
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

/**
 * 获取调蓄池水电表列表
 */
function get_csdt_gate_list() {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var wt = plus.nativeUI.showWaiting("正在加载...");
	var uid = plus.storage.getItem('uid');
	var csdts = plus.storage.getItem('csdts');
	var xxx_url = ip_port + '/Manage/Mobile/get_gate?model=csdt&uid='+uid+'&csdts='+csdts;
	mui.ajax(xxx_url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			add_csdt_gate_item_from_list(data);
			wt.close();
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

/**
 * 添加调蓄池水电表
 * @param {Object} list
 */
function add_csdt_gate_item_from_list(list) {
	var str = '';
	for(var i = 0; i < list.length; i++) {
		var obj = list[i];
		var color = '';
		if(obj.status == 1){
			color = ' style="background:#ADFF2F;"';
		}else if(obj.status == -2){
			color = ' style="background:#FFFF00;"';
		}
		str += '<li class="mui-table-view-cell mui-media mui-col-xs-6 mui-col-sm-6" '+color+'>';
		str += '<a href="txc_gate.html?model=' + obj.model + '&id=' + obj.id + '">';
		str += '<span class="mui-icon">';
		str += '<img src="image/zm.png" />';
		str += '</span>';
		str += '<div class="mui-media-body" style="font-size:20px;height:60px;line-height:30px;">' + obj.station + '</span><br><span>' + obj.gate_name + '</div>';
		str += '</a>';
		str += '</li>';
	}
	document.getElementById('ul_li').innerHTML += str;
}

/**
 * 获取调蓄池水电表对象
 */
function get_csdt_gate_json_obj(model, id) {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var wt = plus.nativeUI.showWaiting("正在加载...");
	var uid = plus.storage.getItem('uid');
	var temp_url = ip_port + '/Manage/Mobile/get_json_obj?model=' + model + '&id=' + id+'&uid='+uid;
	mui.ajax(temp_url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			document.getElementById('station').innerText += data.station;
			document.getElementById('number').innerText += data.gate_name;
			document.getElementById('pid').value += data.id;
			document.getElementById('model').value += $_GET['model'];
			//			set_statuss(data.statuss);
			set_dvice_status_switch(data.status);
			wt.close();
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

/**
 * 获取调蓄池水电表列表
 */
function get_gate_list() {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var wt = plus.nativeUI.showWaiting("正在加载...");
	var uid = plus.storage.getItem('uid');
	var gates = plus.storage.getItem('gates');
	var xxx_url = ip_port + '/Manage/Mobile/get_gate?model=gate&uid='+uid+'&gates='+gates;
	mui.ajax(xxx_url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			add_gate_item_from_list(data);
			wt.close();
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

/**
 * 添加调蓄池水电表
 * @param {Object} list
 */
function add_gate_item_from_list(list) {
	var str = '';
	for(var i = 0; i < list.length; i++) {
		var obj = list[i];
		var color = '';
		if(obj.status == 1){
			color = ' style="background:#ADFF2F;"';
		}else if(obj.status == -2){
			color = ' style="background:#FFFF00;"';
		}
		str += '<li class="mui-table-view-cell mui-media mui-col-xs-6 mui-col-sm-6"'+color+'>';
		str += '<a href="gate.html?model=' + obj.model + '&id=' + obj.id + '">';
		str += '<span class="mui-icon">';
		str += '<img src="image/zm.png" />';
		str += '</span>';
		str += '<div class="mui-media-body" style="font-size:20px;height:60px;line-height:30px;">' + obj.gate_name + '</span><br><span>' + obj.gate_number + '</div>';
		str += '</a>';
		str += '</li>';
	}
	document.getElementById('ul_li').innerHTML += str;
}

/**
 * 获取调蓄池水电表对象
 */
function get_gate_json_obj(model, id) {
	if(!get_work_status()){
		mui.alert(sign_tip, "打卡提示");
		return;
	}
	var wt = plus.nativeUI.showWaiting("正在加载...");
	var uid = plus.storage.getItem('uid');
	var temp_url = ip_port + '/Manage/Mobile/get_json_obj?model=' + model + '&id=' + id+'&uid='+uid;
	mui.ajax(temp_url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			document.getElementById('station').innerText += data.gate_name;
			document.getElementById('number').innerText += data.gate_number;
			document.getElementById('pid').value += data.id;
			document.getElementById('model').value += $_GET['model'];
			//			set_statuss(data.statuss);
			set_dvice_status_switch(data.status);
			wt.close();
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

function get_station_list(model) {
	var wt = plus.nativeUI.showWaiting("正在加载...");
	var uid = plus.storage.getItem('uid');
	var stations = plus.storage.getItem('stations');
	var csdts = plus.storage.getItem('csdts');
	var gates = plus.storage.getItem('gates');
	var xxx_url = ip_port + '/Manage/Mobile/get_facilities?model=' + model+'&uid='+uid+'&stations='+stations+'&csdts='+csdts+'&gates='+gates;
	mui.ajax(xxx_url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			add_facilities_item_from_list(data, 'model=' + model+'&uid='+uid+'&stations='+stations+'&csdts='+csdts+'&gates='+gates);
			wt.close();
			var xxx_url = '/Manage/Mobile/get_last_weather?'+'model=' + model+'&uid='+uid+'&stations='+stations+'&csdts='+csdts+'&gates='+gates+'&f_id='+document.getElementById('facilities_id').value;
			get_weather_text(xxx_url);
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

function add_facilities_item_from_list(data, query) {
	var str = '<select name="facilities_id" id="facilities_id">';
	for(var i = 0; i < data.length; i++) {
		var obj = data[i];
		//		str += '<label class="con-label" for="status_'+obj.status+'">'+obj.comment+'</label>';
//		str += obj.name;
//		if(i == 0) {
//			str += '<input class="status-input" name="facilities_id" type="radio" id="facilities_id_' + obj.id + '" value="' + obj.id + '" checked="checked">';
//		} else {
//			str += '<input class="status-input" name="facilities_id" type="radio" id="facilities_id_' + obj.id + '" value="' + obj.id + '">';
//		}
		str += '<option value="'+obj.id+'">'+obj.name+'</option>';

	}
	str += '</select>';
	var f_id = -1;
	if(data.length>0){
		f_id = data[0].id;
	}else{
		f_id = 0;
	}
	document.getElementById('station').innerHTML += str;
	$('#facilities_id').change(function(){
		get_weather_text('/Manage/Mobile/get_last_weather?'+query+'&f_id='+document.getElementById('facilities_id').value);
	});

	
}

function get_weather_text(url){
	mui.ajax(ip_port+url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		success: function(data) {
			document.getElementById('weather_text').innerHTML = data.msg;
		},
		error: function(edata) {
			console.log(edata.responseText);
			mui.alert(error_tip, "提示");
		}
	});
}

function submit_weather(rain_sit) {
	document.getElementById('rain_sit').value = rain_sit;
	var data = $('#save_form').serializeJson();
	data['c_k'] = '!!PSapp';
	var uid = plus.storage.getItem('uid');
	var temp_url = ip_port + '/Manage/mobile/submit_weather?aa=11&uid='+uid;
	mui.ajax(temp_url, {
		'data': data,
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		//		headers:{'Content-Type':'application/json'},
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			if(data.status > 0) {
				mui.alert(data.msg, "提示");
			} else {
				mui.alert(data.msg, "错误提示");
			}
			history.go(-1);
		},
		error: function() {
			mui.alert(error_tip, "提示");
		}
	});
}

/**
 * 设定开关故障的switch
 * @param {Object} status
 */
function set_dvice_status_switch(status) {
	var active_class = ' mui-active';
	document.getElementById('status_row').style.color = '#ffffff';
	if(status == 1) {
		document.getElementById('status_row').innerHTML += '运行中';
		document.getElementById('status_row').style.background = '#ff0000';
		document.getElementById('open_off').className += active_class;
		//		document.getElementById('open_off').parentNode.querySelector('span').innerText = '开机';
	} else if(status == -2) {
		document.getElementById('status_row').innerHTML += '故障';
		document.getElementById('status_row').style.background = '#ff0000';
		document.getElementById('fault_row').style.visibility = "hidden";
		document.getElementById('fault_status').className += active_class;
		//		document.getElementById('fault_status').parentNode.querySelector('span').innerText = '故障';
	} else if(status == 0 || status == 999) {
		document.getElementById('status_row').innerHTML += '停止';
		document.getElementById('status_row').style.background = '#ff0000';
		//		document.getElementById('open_off').parentNode.querySelector('span').innerText = '关机';
	}
}

function dvice_status_toggle() {
	document.getElementById('open_off').addEventListener('toggle', function(event) {
		//event.detail.isActive 可直接获取当前状态
		//		this.parentNode.querySelector('span').innerText = (event.detail.isActive ? '开机' : '关机');
		document.getElementById('status').value = (event.detail.isActive ? 1 : 0);
		save_form();
	});
	document.getElementById('fault_status').addEventListener('toggle', function(event) {
		//event.detail.isActive 可直接获取当前状态
		//		this.parentNode.querySelector('span').innerText = (event.detail.isActive ? '故障' : '');
		document.getElementById('status').value = (event.detail.isActive ? -2 : '');
		if(event.detail.isActive) {
			var btnArray = ['确定','取消'];
			mui.prompt('请填写故障描述', '', '故障报修', btnArray, function(e) {
				if (e.index == 0) {
					send_fix_data(e.value);
				} else {
					history.go(-1);
				}
			})
//			save_form();
		} else {

		}
	});
}

function dvice_gate_status_tooggle() {
	document.getElementById('open_off').addEventListener('toggle', function(event) {
		//event.detail.isActive 可直接获取当前状态
//		this.parentNode.querySelector('span').innerText = (event.detail.isActive ? '开机' : '关机');
		document.getElementById('status').value = (event.detail.isActive ? 1 : 0);
	});
	document.getElementById('fault_status').addEventListener('toggle', function(event) {
		//event.detail.isActive 可直接获取当前状态
//		this.parentNode.querySelector('span').innerText = (event.detail.isActive ? '故障' : '');
		document.getElementById('status').value = (event.detail.isActive ? -2 : '');
		if(event.detail.isActive) {
			var btnArray = ['确定','取消'];
			mui.prompt('请填写故障描述', '', '故障报修', btnArray, function(e) {
				if (e.index == 0) {
					send_fix_data(e.value);
				} else {
					history.go(-1);
				}
			})
		} else {

		}
	});
}

function send_fix_data(content){
	var post_data = {};
	post_data['model'] = document.getElementById('model').value;
	post_data['station'] = document.getElementById('station').innerText;
	post_data['fy'] = document.getElementById('pid').value;
	post_data['content'] = content;
	var uid = plus.storage.getItem('uid');
	post_data['uid'] = uid;
	post_data['c_k'] = '!!PSapp';
	var temp_url = ip_port + '/Manage/mobile/send_fix?aa=11';
	mui.ajax(temp_url, {
		'data': post_data,
		dataType: 'json', 
		type: 'get', 
		async:false,
		success: function(data) {
			if(data.status == -999){
				mui.alert("登录超时请重新登录", "提示");
				window.location = 'login.html';
			}
			if(data.status <= 0) {
				mui.alert(data.msg, "错误提示");
				history.go(-1);
			}
			if(data.status > 0){
				save_form();
			}
		
		},
		error: function(data) {
//			console.log(data.responseText);
			mui.alert(error_tip, "提示");
		}
	});
}


function set_datetime_picker(auto){
	if(auto == 0||auto == '0'){
		return;
	}
	var opr_div = document.getElementById('opr_datetime_div');
	opr_div.style.display = 'block';
	var input_flag = '<input type="hidden" name="auto_flag" value="1"/>';
	opr_div.innerHTML += input_flag;
	var options = {};
	var input_button = document.getElementById('opr_datetime_input');
	input_button.addEventListener('tap', function() {
		var picker = new mui.DtPicker(options);
		picker.show(function(rs) {
			input_button.value = rs.text+':59';
			picker.dispose();
		});
	}, false);
	var picker = new mui.DtPicker(options);
	picker.show(function(rs) {
		input_button.value = rs.text+':59';
		picker.dispose();
	});
}

//加密函数
function get_scrit(){
	var pix = 'thisisapppix%&|';
	var curtime = new Date().getTime();
	return {'time':curtime,'scrit':hex_md5(pix+curtime)};
}

function logout_clean_cache(){
	plus.storage.removeItem('work_status');
	plus.storage.removeItem('uid');
	plus.storage.removeItem('realname');
	plus.storage.removeItem('stations');
	plus.storage.removeItem('csdts');
	plus.storage.removeItem('gates');
}
